﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;


namespace RISMOTAXDP.EmailService
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : System.Configuration.Install.Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
        }

        private void EmailServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
        {
            //StartService();
        }



        private void EmailServiceInstaller_BeforeRollback(object sender, InstallEventArgs e)
        {
            StopService();
        }

        private void EmailServiceInstaller_BeforeInstall(object sender, InstallEventArgs e)
        {
            StopService();
        }

        private void EmailServiceInstaller_AfterUninstall(object sender, InstallEventArgs e)
        {
            StopService();
        }

        private void EmailServiceInstaller_BeforeUninstall(object sender, InstallEventArgs e)
        {
            StopService();
        }

        private void EmailServiceInstaller_AfterRollback(object sender, InstallEventArgs e)
        {
            StopService();
        }

        #region Helper
        /// <summary>
        /// 启动服务
        /// </summary>
        private void StartService()
        {
            //throw new NotImplementedException();

            ServiceController serverContorler = new ServiceController(this.EmailServiceInstaller.ServiceName);
            if (serverContorler != null)
            {
                if (serverContorler.Status != ServiceControllerStatus.Running)
                {
                    serverContorler.Start();
                    serverContorler.Dispose();
                }
            }
        }

        /// <summary>
        /// 停止服务
        /// </summary>
        private void StopService()
        {
            ServiceController serverContorler = new ServiceController(this.EmailServiceInstaller.ServiceName);
            if (serverContorler != null)
            {
                if (serverContorler.CanStop)
                {
                    serverContorler.Stop();
                    serverContorler.Dispose();
                }
            }

        }
        #endregion

    }
}
